<?php

class SportsController extends AppController{
    
    public $uses = array('User', 'Sport', 'UsersVideo');
    /**
     *  Cette vue permet d'afficher l'acceuil d'un sport
     * @param type $sportId 
     */
    public function view($sportId){
        $data = $this->Sport->find('first', array(
            'conditions'=> array(
                'Sport.id' => $sportId
            ),
            'recursive' => 0
        )); 
        
        $this->changeCurrentSport($sportId, false);
        
        $lastVideosAdvices = $this->getLastVideosAdvice($sportId, 10);
        
        $lastVideosEditings = $this->getLastVideosEditings($sportId);
        $this->set('lastVideosEditings',$lastVideosEditings);
        
        
        $lastVideosTricks = $this->getLastVideosTricks($sportId);
        $this->set('lastVideosTricks',$lastVideosTricks);
        
        $lastVideosTutorials = $this->getLastVideosTutorials($sportId);
        $this->set('lastVideosTutorials',$lastVideosTutorials);
        
        $this->set("sport", $data);
        $this->set("lastVideosAdvices", $lastVideosAdvices);
    }
    
    /**
     *  Page comportant les information sur un sport
     * @param type $sportId 
     */
    public function description($sportId){
        $this->set("sport", $this->getSport($sportId));
    }
    
    /**
     * Fonction de récupération des données
     */
    
    /**
     * Retourne le sport d'id $id
     * @param type $id
     * @return array 
     */
    public function getSport($id){
        return $this->Sport->find('first', array(
            'conditions' => array(
                'Sport.id' => $id
            )
        ));
    }
    /**
     * Retourne tout les sports de la base de donnée
     * @return array 
     */
    public function getSports(){
        return $this->Sport->find('all');
    }
    
    
    
    
    
    
    
    /**
     * TODO Fonction accessible seulement au administrateurs!
     */
    
    public function admin_index(){
        $sports = $this->getSports();
        
        $this->set("sports", $sports);
    }
    
    public function admin_del_sport($sportId = null){
        $this->Sport->delete($sportId);
        $this->Session->setFlash("The sport has been delete");
        $this->redirect(array('action' => 'admin_edit'));
    }
    public function admin_edit($sportId = null){
        
        if (!empty($this->request->data['Sport'])){
                $data = $this->request->data['Sport'];
                $data['id'] = $sportId;
                $this->Sport->save($data);           
        } 
        
        if ($sportId != null){
            if (!empty($this->request->data['Trick'])){
                $data = $this->request->data['Trick'];
                $data['sports_id'] = $sportId;
                $this->Sport->Trick->save($data);
            }
            else if (!empty($this->request->data['TricksType'])){
                $data = $this->request->data['TricksType'];    
                $data['sports_id'] = $sportId;
                $this->Sport->TricksType->save($data);
            }
        
            $sport = $this->getSport($sportId);
            $this->set("sport", $sport);            
        
        }
        else{
            $listSports = $this->getListSports();
            $this->set('listSports', $listSports);
        }
        
        

    }
    
    public function getListSports(){
        return $this->Sport->find('list', array(
            'order' => 'Sport.id'
            )
        );
    }
    
    
    public function getLastVideosAdvice($sportId, $nb = 10){
        return $this->User->UsersVideo->VideosAdvice->find('all', array(
            'conditions' => array(
                'UsersVideo.sports_id' => $sportId
            ),
            'limit' => $nb,
            'order' => 'UsersVideo.posted_on'
        ));
    }
    
    public function getLastVideosTutorials($sportId, $nb = 10){
        return $this->UsersVideo->getVideos('all', array(
            'conditions' => array(
                'UsersVideo.type' => 'VideosTutorial', 
                'UsersVideo.sports_id' => $sportId
            ),
            'limit' => $nb,
            'order' => 'UsersVideo.posted_on',
            'recursive' => 0
        ));
    }
    
    
    public function getLastVideosTricks($sportId, $nb = 10){
        return $this->UsersVideo->getVideos('all', array(
            'conditions' => array(
                'UsersVideo.type' => 'VideosTrick', 
                'UsersVideo.sports_id' => $sportId
            ),
            'limit' => $nb,
            'order' => 'UsersVideo.posted_on',
            'recursive' => 0
        ));
    }
    
    
    public function getLastVideosEditings($sportId, $nb = 10){
        return $this->UsersVideo->getVideos('all', array(
            'conditions' => array(
                'UsersVideo.type' => 'VideosEditing', 
                'UsersVideo.sports_id' => $sportId
            ),
            'limit' => $nb,
            'order' => 'UsersVideo.posted_on',
            'recursive' => 0
        ));
    }
    
    
    
    
    
    public function information($sportId){
        $this->set('data', $this->getSport($sportId));
    }
    
    public function beforeFilter(){
        parent::beforeFilter();
        $this->Auth->allow('*');
    }
}
?>
